package cn.lf.android.lawfirm.undertake.apply;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.autolayout.AutoRelativeLayout;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.BaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.undertake.apply.controler.CaseTypeConroller;
import cn.lf.android.lawfirm.undertake.apply.modle.Approver;
import cn.lf.android.lawfirm.undertake.apply.modle.CaseType;
import cn.lf.android.lawfirm.undertake.apply.modle.CaseTypeTwo;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFee;
import cn.lf.android.lawfirm.undertake.approval.modle.LawyerFeeC;
import cn.lf.android.lawfirm.uploadcontroller.UpLodeController;
import cn.lf.android.lawfirm.downloadcontroller.modle.Agreement;
import cn.lf.android.lawfirm.downloadcontroller.view.AgreementAdapter;
import cn.lf.android.lawfirm.undertake.apply.controler.ApplyUnderConroller;
import cn.lf.android.lawfirm.undertake.apply.modle.BasicFee;
import cn.lf.android.lawfirm.undertake.apply.modle.CaseLevel;
import cn.lf.android.lawfirm.undertake.apply.modle.UndertakeApply;
import cn.lf.android.lawfirm.undertake.approval.AddLawyerFeeActivity;
import cn.lf.android.lawfirm.uploadcontroller.view.UpAgreementAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.GetPathUtil;
import cn.lf.android.lawfirm.utils.timepicker.TimePicker;
import cn.lf.android.lawfirm.view.NoScrollListView;
import cn.lf.android.lawfirm.view.OptionDialog;
import cn.lf.android.lawfirm.view.pickerview.OptionsPopupWindow;
import cn.lf.android.lawfirm.worklog.AddWorkLogActivity;

import static android.R.attr.bitmap;
import static android.R.attr.value;
import static android.R.id.list;
import static cn.lf.android.lawfirm.MyApplication.context;
import static cn.lf.android.lawfirm.R.id.options1;
import static cn.lf.android.lawfirm.utils.GetPathUtil.getPathByUri4kitkat;

/**
 * 承办申请
 */
public class ApplyUndertakeActivity extends BaseActivity implements ApplyUnderConroller.ApplyUnderInfoCallback, ApplyUnderConroller.UndertakeSubmitCallback, UpLodeController.UpLodeListCallback, CaseTypeConroller.CaseTypeCallback {

    /**
     * 返回
     **/
    private ImageButton img_left;
    /**
     * 头
     **/
    private TextView mtitle, eb_tv_right;
    private ApplyUnderConroller mApplyUnderConroller;
    //--合同文件
    private List<Agreement> mListFileInfo = new ArrayList<Agreement>();
    private NoScrollListView file_list;
    private UpAgreementAdapter mAgreementAdapter;
    //---
    private String approvesid = "";
    private TextView three_name, three_value, caset_type, lawyer_fee, name, slient, opposing_party, matter, team, charge_mode, pay_way, lawyerfee, baselawyer_fee, choice_level, time_lawfee, approver;

    private EditText total, fee_remarks, apply_remarks, time, benchmark_cost;
    private AutoRelativeLayout rl_three, rl_approver, rl_casetype, rl_charge_mode, rl_payway, rl_lawyer_fee, rl_baselawyer_fee, rl_choice_level, rl_benchmark_cost, rl_time, rl_time_fee;
    //--
    private TimePicker mTimePicker_shenji;
    private List<CaseLevel> shenjiList = new ArrayList<CaseLevel>();
    public String GUID = "";//审级编号

    private TimePicker mTimePicker_basicFees;
    private List<BasicFee> basicFeesList = new ArrayList<BasicFee>();

    private TimePicker mTimePicker_shoufei;
    private String id_shoufei = "";
    private TimePicker mTimePicker_fukuan1, mTimePicker_fukuan2, mTimePicker_fukuan3;
    private String id_fukuan = "";

    private TimePicker mTimePicker_shenpiren;//审批人
    private List<Approver> shenpirenList = new ArrayList<Approver>();

    private TimePicker mTimePicker_three;//三级案件类型
    //------参数----
//    private String entry_name = "";//款项名称
//    private String shoufei_time = "";//收费时间
//    private String shoukuan_tiaojian = "";//收款条件
    private String caseTypeOneStr = "";
    private String approveman = "";//审批人编号
    private String caseID = "";
    private String caseTypeID = "";//案件类型（必填）
    private ImageView add_file;
    private UpLodeController mUpLodeController;
    private CaseTypeConroller mCaseTypeConroller;
    private OptionsPopupWindow pwOptionscasetype;
    private ArrayList<String> options1_major = new ArrayList<String>();
    private ArrayList<ArrayList<String>> options2_major = new ArrayList<ArrayList<String>>();
    private String CaseOtherType = "-1";
    /**
     * 上传
     **/
    int SELECT = 1001;

    @Override
    public Object getContentResView() {
        return R.layout.activity_apply_undertake;
    }

    @Override
    public void initView() {
        initTitle();
        caseID = getIntent().getStringExtra("caseID");

        name = (TextView) findViewById(R.id.name);
        slient = (TextView) findViewById(R.id.slient);
        opposing_party = (TextView) findViewById(R.id.opposing_party);
        matter = (TextView) findViewById(R.id.matter);
        team = (TextView) findViewById(R.id.team);
        charge_mode = (TextView) findViewById(R.id.charge_mode);
        pay_way = (TextView) findViewById(R.id.pay_way);
        lawyerfee = (TextView) findViewById(R.id.lawyerfee);
        baselawyer_fee = (TextView) findViewById(R.id.baselawyer_fee);
        choice_level = (TextView) findViewById(R.id.choice_level);
        time_lawfee = (TextView) findViewById(R.id.time_lawfee);
        approver = (TextView) findViewById(R.id.approver);
        caset_type = (TextView) findViewById(R.id.caset_type);
        lawyer_fee = (TextView) findViewById(R.id.lawyer_fee);
        total = (EditText) findViewById(R.id.total);
        fee_remarks = (EditText) findViewById(R.id.fee_remarks);
        apply_remarks = (EditText) findViewById(R.id.apply_remarks);
        time = (EditText) findViewById(R.id.time);
        benchmark_cost = (EditText) findViewById(R.id.benchmark_cost);
        three_name = (TextView) findViewById(R.id.three_name);
        three_value = (TextView) findViewById(R.id.three_value);
        rl_three = (AutoRelativeLayout) findViewById(R.id.rl_three);
        rl_three.setOnClickListener(this);
        rl_approver = (AutoRelativeLayout) findViewById(R.id.rl_approver);
        rl_approver.setOnClickListener(this);
        rl_charge_mode = (AutoRelativeLayout) findViewById(R.id.rl_charge_mode);
        rl_charge_mode.setOnClickListener(this);
        rl_casetype = (AutoRelativeLayout) findViewById(R.id.rl_casetype);
        rl_casetype.setOnClickListener(this);
        rl_payway = (AutoRelativeLayout) findViewById(R.id.rl_payway);
        rl_payway.setOnClickListener(this);
        rl_lawyer_fee = (AutoRelativeLayout) findViewById(R.id.rl_lawyer_fee);
        rl_lawyer_fee.setOnClickListener(this);
        rl_baselawyer_fee = (AutoRelativeLayout) findViewById(R.id.rl_baselawyer_fee);
        rl_baselawyer_fee.setOnClickListener(this);
        rl_choice_level = (AutoRelativeLayout) findViewById(R.id.rl_choice_level);
        rl_choice_level.setOnClickListener(this);
        rl_benchmark_cost = (AutoRelativeLayout) findViewById(R.id.rl_benchmark_cost);
        rl_time = (AutoRelativeLayout) findViewById(R.id.rl_time);
        rl_time_fee = (AutoRelativeLayout) findViewById(R.id.rl_time_fee);
        add_file = (ImageView) findViewById(R.id.add_file);
        add_file.setOnClickListener(this);
        file_list = (NoScrollListView) findViewById(R.id.file_list);
        mAgreementAdapter = new UpAgreementAdapter(ApplyUndertakeActivity.this, mListFileInfo, "承办申请");
        file_list.setAdapter(mAgreementAdapter);
        setlistener();

        List<String> list_three = new ArrayList<String>();
        list_three.add("常年法律顾问");
        list_three.add("专项法律顾问");
        mTimePicker_three = new TimePicker(this, 1, list_three,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        three_value.setText(timeString);
                        if (timeString.equals("常年法律顾问")) {
                            CaseOtherType = "3";
                        } else {
                            CaseOtherType = "4";
                        }
                    }
                });
    }


    private void initTitle() {
        img_left = (ImageButton) findViewById(R.id.img_left);
        img_left.setVisibility(View.VISIBLE);
        img_left.setOnClickListener(this);
        mtitle = (TextView) findViewById(R.id.eb_tv_title);
        mtitle.setVisibility(View.VISIBLE);
        mtitle.setText("承办申请");
        eb_tv_right = (TextView) findViewById(R.id.eb_tv_right);
        eb_tv_right.setOnClickListener(this);
        eb_tv_right.setVisibility(View.VISIBLE);
        eb_tv_right.setText("确认");
    }

    public void setlistener() {
        pwOptionscasetype = new OptionsPopupWindow(this);
        pwOptionscasetype.setTextSize(45);
        pwOptionscasetype.setSingleLine(true);
        pwOptionscasetype.setMaxTextItem(11);
        pwOptionscasetype.setOutsideTouchable(true);
        // 监听确定选择按钮
        pwOptionscasetype
                .setOnoptionsSelectListener(new OptionsPopupWindow.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2,
                                                int options3) {
                        // 返回的分别是三个级别的选中位置
                        caset_type.setText(options2_major.get(options1).get(
                                option2));
                        caseTypeOneStr = options1_major.get(options1);
                        if ("法律顾问".equals(options1_major.get(options1))) {
                            rl_three.setVisibility(View.VISIBLE);
                            three_name.setText("其他");
                        } else {
                            CaseOtherType = "-1";
                            rl_three.setVisibility(View.GONE);
                        }
                        caseTypeID = typebeans.get(options1).data.get(option2).guid;
                        if (!typebeans.get(options1).data.get(option2).isNonLitigation) {
                            rl_choice_level.setVisibility(View.GONE);
                        } else {
                            rl_choice_level.setVisibility(View.VISIBLE);
                        }

                    }
                });
        benchmark_cost.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String ss = s.toString().trim();
                String sss = time.getText().toString().trim();
                if (ss.length() > 0 && sss.length() > 0) {
                    time_lawfee.setText("" + Integer.valueOf(ss) * Integer.valueOf(sss));
                }
            }
        });
        time.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String ss = s.toString().trim();
                String sss = benchmark_cost.getText().toString().trim();
                if (ss.length() > 0 && sss.length() > 0) {
                    time_lawfee.setText("" + Integer.valueOf(ss) * Integer.valueOf(sss));
                }
            }
        });
    }

    @Override
    public void initData() {
        //滑动退出
//        addSwipeFinishLayout();
        mApplyUnderConroller = new ApplyUnderConroller(this, this, this);
        mApplyUnderConroller.onApplyUnderInfo(caseID);
        initTimePiker();
        mUpLodeController = new UpLodeController(this, this);
        mCaseTypeConroller = new CaseTypeConroller(this, this);
    }

    List<String> list_fukuan = new ArrayList<String>();

    public void initTimePiker() {

        List<String> list_shoufei = new ArrayList<String>();
        list_shoufei.add("定额收费");
        list_shoufei.add("计时收费");
        list_shoufei.add("风险收费");
        mTimePicker_shoufei = new TimePicker(this, 1, list_shoufei,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        charge_mode.setText(timeString);
                        if ("定额收费".equals(timeString)) {
                            list_fukuan.clear();
                            list_fukuan.add("一次性");
                            list_fukuan.add("分期支付");
                            if (null == mTimePicker_fukuan1) {
                                onInitTimepicker(1);
                            }
                            rl_benchmark_cost.setVisibility(View.GONE);
                            rl_time.setVisibility(View.GONE);
                            rl_time_fee.setVisibility(View.GONE);
                            id_shoufei = "1";
                        } else if ("计时收费".equals(timeString)) {
                            list_fukuan.clear();
                            list_fukuan.add("单一标准");
                            list_fukuan.add("不同标准");
                            if (null == mTimePicker_fukuan2) {
                                onInitTimepicker(2);
                            }
                            id_shoufei = "2";
                            rl_benchmark_cost.setVisibility(View.VISIBLE);
                            rl_time.setVisibility(View.VISIBLE);
                            rl_time_fee.setVisibility(View.VISIBLE);
                        } else if ("风险收费".equals(timeString)) {
                            list_fukuan.clear();
                            list_fukuan.add("全风险");
                            list_fukuan.add("半风险");
                            if (null == mTimePicker_fukuan3) {
                                onInitTimepicker(3);
                            }
                            id_shoufei = "3";
                            rl_benchmark_cost.setVisibility(View.GONE);
                            rl_time.setVisibility(View.GONE);
                            rl_time_fee.setVisibility(View.GONE);
                        }
                    }
                });

    }

    private void onInitTimepicker(int i) {
        if (i == 1) {
            mTimePicker_fukuan1 = new TimePicker(this, 1, list_fukuan,
                    new TimePicker.OnPickerCallBack() {
                        @Override
                        public void pickerDone(String timeString, long timeInMillis) {
                            pay_way.setText(timeString);
                            if ("一次性".equals(timeString)) {
                                id_fukuan = "1";
                            } else if ("分期支付".equals(timeString)) {
                                id_fukuan = "2";
                            }
                        }
                    });
        }
        if (i == 2) {
            mTimePicker_fukuan2 = new TimePicker(this, 1, list_fukuan,
                    new TimePicker.OnPickerCallBack() {
                        @Override
                        public void pickerDone(String timeString, long timeInMillis) {
                            pay_way.setText(timeString);
                            if ("单一标准".equals(timeString)) {
                                id_fukuan = "3";
                            } else if ("不同标准".equals(timeString)) {
                                id_fukuan = "4";
                            }
                        }
                    });
        }
        if (i == 3) {
            mTimePicker_fukuan3 = new TimePicker(this, 1, list_fukuan,
                    new TimePicker.OnPickerCallBack() {
                        @Override
                        public void pickerDone(String timeString, long timeInMillis) {
                            pay_way.setText(timeString);
                            if ("全风险".equals(timeString)) {
                                id_fukuan = "5";
                            } else if ("半风险".equals(timeString)) {
                                id_fukuan = "6";
                            }
                        }
                    });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_three:
                hintKbTwo();
                mTimePicker_three.show(getWindow().getDecorView());
                break;
            case R.id.rl_approver:
                hintKbTwo();
                mTimePicker_shenpiren.show(getWindow().getDecorView());
                break;
            case R.id.rl_casetype:
                hintKbTwo();
                pwOptionscasetype.showAtLocation(findViewById(R.id.ll_linearlayout),
                        Gravity.BOTTOM, 0, 0);
                break;
            case R.id.add_file:
                hintKbTwo();
                Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
                intent1.setType("application/msword");//
                if (Build.VERSION.SDK_INT >= 23) {// 判断版本
                    if (ContextCompat.checkSelfPermission(
                            ApplyUndertakeActivity.this,
                            Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                            || ContextCompat.checkSelfPermission(mActivity,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat
                                .requestPermissions(
                                        ApplyUndertakeActivity.this,
                                        new String[]{
                                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                                Manifest.permission.WRITE_EXTERNAL_STORAGE},
                                        8);
                    } else {
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                            startActivityForResult(intent1, SELECT);
                        } else {
                            startActivityForResult(intent1, SELECT);
                        }
                    }

                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        startActivityForResult(intent1, SELECT);
                    } else {
                        try {
                            startActivityForResult(intent1, SELECT);
                        } catch (Exception f) {
                            Toast.makeText(this, "不支持application/msword文件查找", Toast.LENGTH_LONG).show();
                        }

                    }
                }
                break;
            case R.id.img_left:
                hintKbTwo();
                finish();
                break;
            case R.id.eb_tv_right://确认
                hintKbTwo();
                if (check()) {
                    String filestr = "";
                    for (Agreement agreement : mListFileInfo) {
                        filestr = filestr + agreement.fileID + ",";
                    }
                    String feestr = "";
                    for (LawyerFee lawyerfee : mBean) {
                        feestr = feestr + lawyerfee.paymentName + "," + lawyerfee.shouldCharge + "," + lawyerfee.expectGatheringDate + "," + lawyerfee.condition + ",";
                    }
//                    for (int i = 0; i < mBean.size(); i++) {
//                        if (i == mBean.size() - 1) {
//                            feestr = feestr + mBean.get(i).paymentName + "," + mBean.get(i).shouldCharge + "," + mBean.get(i).expectGatheringDate + "," + mBean.get(i).condition;
//                        } else {
//                            feestr = feestr + mBean.get(i).paymentName + "," + mBean.get(i).shouldCharge + "," + mBean.get(i).expectGatheringDate + "," + mBean.get(i).condition + ",";
//                        }
//                    }
                    String ss = feestr.replaceAll("，", "★");
                    DebugLog.e("ssc==" + ss);
                    String postStr = GUID + "," + id_shoufei + "," + id_fukuan + "," + fee_remarks.getText().toString().trim() + "," + total.getText().toString() + "," + benchmark_cost.getText().toString().trim() + "," + time.getText().toString().trim() + "," + time_lawfee.getText().toString().trim() + "," + ss;
                    mApplyUnderConroller.onSubmit(caseID, approveman, apply_remarks.getText().toString().trim(), GUID, baselawyer_fee.getText().toString(), postStr, filestr, caseTypeID, CaseOtherType);
                }
                break;
            case R.id.rl_charge_mode://收费方式
                hintKbTwo();
                mTimePicker_shoufei.show(getWindow().getDecorView());
                break;
            case R.id.rl_payway://付款方式
                hintKbTwo();
                if (list_fukuan.size() == 0) {
                    showCToast("请选先择收费方式");
                    return;
                }
                if (id_shoufei.equals("1")) {
                    mTimePicker_fukuan1.show(getWindow().getDecorView());
                } else if (id_shoufei.equals("2")) {
                    mTimePicker_fukuan2.show(getWindow().getDecorView());
                } else if (id_shoufei.equals("3")) {
                    mTimePicker_fukuan3.show(getWindow().getDecorView());
                }
                break;
            case R.id.rl_baselawyer_fee://基本律师费
                hintKbTwo();
                mTimePicker_basicFees.show(getWindow().getDecorView());
                break;
            case R.id.rl_choice_level://选择审级
                hintKbTwo();
                mTimePicker_shenji.show(getWindow().getDecorView());
                break;
            case R.id.rl_lawyer_fee://律师费
                hintKbTwo();
                Intent intent2 = new Intent(ApplyUndertakeActivity.this, AddLawyerFeeActivity.class);
                LawyerFeeC b = new LawyerFeeC();
                b.bean = mBean;
                intent2.putExtra("lawyerfee", b);
                intent2.putExtra("from", "ApplyUndertakeActivity");
                startActivityForResult(intent2, 1002);
                break;
            default:
                break;
        }
    }

    List<LawyerFee> mBean = new ArrayList<LawyerFee>();

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {
        if (requestCode == 8) {// 读取文件
            if (grantResults[0] == 0) {// 允许
                Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);
                intent1.setType("application/msword");//application/msword
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    startActivityForResult(intent1, SELECT);
                } else {
                    startActivityForResult(intent1, SELECT);
                }
            } else {// 拒绝
//                if (!shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)
//                        || !shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                giveUpShow("读取文件失败，请检查设备并开启权限");
//                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 1002) {//律师费
            mBean.clear();
            LawyerFeeC b = (LawyerFeeC) data.getSerializableExtra("lawyerfee");
            mBean.addAll(b.bean);
            if (mBean.size() > 0) {
                lawyer_fee.setText("已添加");
            } else {
                lawyer_fee.setText("未添加");
            }
        }
        if (requestCode == SELECT) {//
            if (resultCode == RESULT_OK && data != null) {
                Uri uri = data.getData();
                DebugLog.e("86666==" + uri);
                DebugLog.e("86666==" + uri.getEncodedPath());
                DebugLog.e("86666==" + uri.getPath());
                mUpLodeController.onUpLode(caseID, "1", new GetPathUtil().getPath(this, uri));
            }
        }
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1) {
                Uri uri = data.getData();
//                Intent intent = new Intent("android.intent.action.VIEW");
//                intent.addCategory("android.intent.category.DEFAULT");
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                intent.setDataAndType(uri, "application/msword");
//                startActivity(intent);
                DebugLog.e("76666==" + uri.getPath().toString());
                mUpLodeController.onUpLode(caseID, "1", uri.getPath().toString());
                //                Toast.makeText(this, "文件路径：" + uri.getPath().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private UndertakeApply mbeans;

    @Override
    public void onApplyUnderInfouccessed(UndertakeApply beans) {
        DebugLog.e("beans==" + beans);
        mbeans = beans;
        //-------------审级-----------------
        if ("y".equals(beans.isShowLevel)) {
            rl_choice_level.setVisibility(View.VISIBLE);
            shenjiList.addAll(beans.CaseLevel);
            List<String> list_shenji = new ArrayList<String>();
            for (CaseLevel caseLevel : shenjiList) {
                list_shenji.add(caseLevel.levelName);
            }
            mTimePicker_shenji = new TimePicker(this, 1, list_shenji,
                    new TimePicker.OnPickerCallBack() {
                        @Override
                        public void pickerDone(String timeString, long timeInMillis) {
                            choice_level.setText(timeString);
                            for (CaseLevel bean : shenjiList) {
                                if (timeString.equals(bean.levelName)) {
                                    GUID = bean.GUID;
                                }
                            }
                        }
                    });
        } else if ("n".equals(beans.isShowLevel)) {
            rl_choice_level.setVisibility(View.GONE);
        }

        //-------------基本律师费-----------------
        basicFeesList.addAll(beans.BasicFee);
        List<String> list_basicFees = new ArrayList<String>();
        for (BasicFee basicFee : basicFeesList) {
            list_basicFees.add(basicFee.text);
        }
        mTimePicker_basicFees = new TimePicker(this, 1, list_basicFees,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        baselawyer_fee.setText(timeString);
                    }
                });
        mCaseTypeConroller.onCaseType();
        //-------------审批人-----------------
        shenpirenList.addAll(beans.Approver);
        List<String> list_shenpiren = new ArrayList<String>();
        for (Approver approver : shenpirenList) {
            list_shenpiren.add(approver.LsysUserName);
        }
        mTimePicker_shenpiren = new TimePicker(this, 1, list_shenpiren,
                new TimePicker.OnPickerCallBack() {
                    @Override
                    public void pickerDone(String timeString, long timeInMillis) {
                        approver.setText(timeString);
                        for (Approver bean : shenpirenList) {
                            if (timeString.equals(bean.LsysUserName)) {
                                approveman = bean.LsysUserID;
                            }
                        }
                    }
                });
        mCaseTypeConroller.onCaseType();
        setView(beans);

    }

    public void setView(UndertakeApply beans) {
        name.setText(beans.caseName);
        slient.setText(beans.WeiTuoFang);
        opposing_party.setText(beans.dfdsr);
        matter.setText(beans.commitment);
        team.setText(beans.members);
        caset_type.setText(beans.CaseType.typeName);
        caseTypeID = beans.CaseType.caseTypeID;
        String str = "";
        for (int i = 0; i < beans.Approver.size(); i++) {
            if (i == beans.Approver.size() - 1) {
                str = str + beans.Approver.get(i).LsysUserName;
            } else {
                str = str + beans.Approver.get(i).LsysUserName + "，";
            }
        }
    }

    @Override
    public void onApplyUnderInfoFailed() {

    }

    public boolean check() {
        if (total.getText().toString().trim().length() == 0) {
            showCToast("请输入合同总额");
            return false;
        }
        if (charge_mode.getText().toString().trim().length() == 0) {
            showCToast("请选择收费方式");
            return false;
        } else {
            if ("计时收费".equals(charge_mode.getText().toString())) {
                if (benchmark_cost.getText().toString().trim().length() == 0) {
                    showCToast("请输入基准费用");
                    return false;
                }
                if (time.getText().toString().trim().length() == 0) {
                    showCToast("请输入所用时间");
                    return false;
                }
            }
        }
        if (mBean.size() == 0) {
            showCToast("请添加律师费");
            return false;
        }
        if (baselawyer_fee.getText().toString().trim().length() == 0) {
            showCToast("请选择基本律师费");
            return false;
        }
        if (choice_level.getText().toString().trim().length() == 0&&rl_choice_level.getVisibility()==View.VISIBLE) {
            showCToast("请选择审级");
            return false;
        }
        if (caseTypeID.length() == 0) {
            showCToast("请选择案件类型");
            return false;
        }
        if (approveman.length() == 0) {
            showCToast("请选择审批人");
            return false;
        }
        if (total.getText().toString().trim().length() == 0) {
            showCToast("请输入合同总额");
            return false;
        }
        if ("法律顾问".equals(caseTypeOneStr)) {
            if ("-1".equals(CaseOtherType)) {
                Toast.makeText(this, "您尚未选具体的法律顾问类别（案件类型中其他类别），请检查后重新尝试！", Toast.LENGTH_LONG).show();
                return false;
            }
        }
        int d = Integer.valueOf(total.getText().toString().trim());
        int d2 = 0;
        for (LawyerFee bean : mBean) {
            d2 = d2 + Integer.valueOf(bean.shouldCharge);
        }
        if (d != d2) {
            showCToast("当前收费款项总额与合同额不一致");
            return false;
        }
        return true;
    }

    @Override
    public void onSubmitSuccessed(String str) {
        ApplyUndertakeListActivity.isRefresh = true;
        showCToast("操作成功");
        finish();
    }

    @Override
    public void onSubmitFailed() {
        showCToast("操作失败");
    }

    @Override
    public void onUpLodeListuccessed(List<Agreement> beans) {
        DebugLog.e("上传回来=" + beans);
        mListFileInfo.addAll(beans);
        mAgreementAdapter.notifyDataSetChanged();
    }

    @Override
    public void onUpLodeListFailed() {
        showCToast("上传失败");
    }

    List<CaseTypeTwo> beanstwo = new ArrayList<CaseTypeTwo>();
    List<CaseType> typebeans;

    @Override
    public void onCaseTypeSuccessed(List<CaseType> beans) {

        typebeans = beans;
        for (CaseType casetype : beans) {
            options1_major.add(casetype.subName);
            ArrayList<String> list = new ArrayList<>();
            for (CaseTypeTwo casetypetwo : casetype.data) {
                list.add(casetypetwo.name);
                beanstwo.add(casetypetwo);
            }
            options2_major.add(list);
        }
        pwOptionscasetype.setPicker(options1_major, options2_major, true);
    }

    @Override
    public void onCaseTypeFailed() {

    }

    /**
     * 权限拒绝显示
     */
    private void giveUpShow(String str) {
        final OptionDialog dialog = new OptionDialog(this);
        dialog.setAnimationEnable(true);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_promess,
                null);
        TextView promess = (TextView) view.findViewById(R.id.promess);
        promess.setText(str);
        dialog.setPositiveListener("确定", new OptionDialog.OnPositiveListener() {
            @Override
            public void onClick(OptionDialog dialog) {
                dialog.optionDialogDismiss();
                dialog.dismiss();
            }
        });
        dialog.setCustomView(view);
        dialog.show();
    }


}
